<?php

use Elasticsearch\ClientBuilder;

/**
 * @name IndexController
 * @author root
 * @desc 默认控制器
 * @see http://www.php.net/manual/en/class.yaf-controller-abstract.php
 */
class IndexController extends BaseController
{
    /**
     * 默认初始化方法，如果不需要，可以删除掉这个方法
     * 如果这个方法被定义，那么在Controller被构造以后，Yaf会调用这个方法
     */
    public function init()
    {
//        $this->checkLogin();
//        parent::init(); // TODO: Change the autogenerated stub
    }

    /**
     * 默认动作
     * Yaf支持直接把\Yaf\Request_Abstract::getParam()得到的同名参数作为Action的形参
     * 对于如下的例子, 当访问http://yourhost/demo/index/index/index/name/root 的时候, 你就会发现不同
     */

    use \app\library\traits\Restful;

    public function read()
    {
        echo "index1111";
        return false;

    }

    public function rpcDemoAction()
    {
        $aa = \app\library\rpc\MyRpc::demo(['ss' => 'ddd']);
        var_dump($aa);
        return false;
    }

    public function gorpcAction()
    {
        $client = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);

        $connect = $client->connect('192.168.8.91', 8096, 1);
        if (!$connect) {
            var_dump($client->errCode);
            $client->close();
        }

        $string = json_encode([
            'method' => 'Arith.Multiply',
            'params' => [[
                'A' => 9,
                'B' => 2
            ]],
            'id' => 1
        ]);
        $res = $client->send($string);
        $datas = $client->recv();
        var_dump($datas);
        $client->close();
        return false;
    }

    public function rpcxJsonAction(){

        $client = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);

        $connect = $client->connect('127.0.0.1', 8972, 1);
        if (!$connect) {
            var_dump($client->errCode);
            $client->close();
        }
        var_dump($connect);

        $string = json_encode([
            'method' => 'Arith.Mul',
            'params' => [[
                'A' => 10,
                'B' => 20
            ]],
            'id' => 1
        ]);

        var_dump($string);
        $res = $client->send($string);
        var_dump($res);
        $datas = $client->recv();
        var_dump($datas);
        $client->close();
        return false;
    }

    public function rpcxMsgpackAction(){

        $client = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);

        $connect = $client->connect('127.0.0.1', 8972, 1);
        if (!$connect) {
            var_dump($client->errCode);
            $client->close();
        }
        var_dump($connect);

        $string = msgpack_pack([
            'method' => 'Arith.Mul',
            'params' => [[
                'A' => 10,
                'B' => 20
            ]],
            'id' => 1
        ]);
        var_dump($string);
        $res = $client->send($string);
        var_dump($res);
        $datas = $client->recv();
        var_dump($datas);
        $client->close();
        return false;
    }

    public function thinkphpRpcAction()
    {
        $client = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);

        if (!$client->connect('47.114.32.24', 9502, 1)) {
            var_dump($client->errCode);
        }
        $data = [
            'jsonrpc' => "2.0",
            'method' => "MyRpcInterface@demo",
            'params' => [],
            'id' => '',
        ];

        //        $string = 'D{"jsonrpc":"2.0","method":"MyRpcInterface@demo","params":[],"id":""}';
        $string = json_encode($data, JSON_UNESCAPED_UNICODE);
        var_dump($string);

        //转为二进制数据
        $string = pack('NN', strlen($string), 0) . $string;

        $res = $client->send($string);

        $datas = $client->recv();

        $header = unpack('Nlength/Ntype', substr($datas, 0, 8));
        $data = substr($datas, 8);

        var_dump($header);
        var_dump($data);

        $client->close();
    }


}
